using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace demo
{
    /// <summary>
    /// 消息类型枚举
    /// </summary>
    public enum MessageType
    {
        Login = 1,           // 登录请求
        LoginResponse = 2,   // 登录响应
        Chat = 3,           // 聊天消息
        Broadcast = 4,      // 广播消息
        UserListRequest = 5, // 用户列表请求
        UserListResponse = 6,// 用户列表响应
        UserStatusUpdate = 7,// 用户状态更新
        Error = 8,          // 错误消息
        Heartbeat = 9,      // 心跳消息
        Disconnect = 10,    // 断开连接
        ServerCommand = 11  // 服务器命令
    }

    /// <summary>
    /// 基础消息模型
    /// </summary>
    public class MessageModel
    {
        [JsonProperty("type")]
        public MessageType Type { get; set; }

        [JsonProperty("messageId")]
        public string MessageId { get; set; } = Guid.NewGuid().ToString();

        [JsonProperty("timestamp")]
        public string Timestamp { get; set; } = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");

        [JsonProperty("sender")]
        public string Sender { get; set; }

        [JsonProperty("receiver")]
        public string Receiver { get; set; }

        [JsonProperty("content")]
        public string Content { get; set; }

        [JsonProperty("data")]
        public object Data { get; set; }

        [JsonProperty("success")]
        public bool? Success { get; set; }

        [JsonProperty("error")]
        public string Error { get; set; }
    }

    /// <summary>
    /// 登录消息
    /// </summary>
    public class LoginMessage : MessageModel
    {
        [JsonProperty("username")]
        public string Username { get; set; }

        [JsonProperty("password")]
        public string Password { get; set; }

        [JsonProperty("clientInfo")]
        public object ClientInfo { get; set; }

        public LoginMessage()
        {
            Type = MessageType.Login;
        }
    }

    /// <summary>
    /// 登录响应消息
    /// </summary>
    public class LoginResponseMessage : MessageModel
    {
        [JsonProperty("sessionId")]
        public string SessionId { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }

        [JsonProperty("serverTime")]
        public string ServerTime { get; set; }

        [JsonProperty("onlineUsers")]
        public int OnlineUsers { get; set; }

        [JsonProperty("userInfo")]
        public UserInfo UserInfo { get; set; }

        public LoginResponseMessage()
        {
            Type = MessageType.LoginResponse;
        }
    }

    /// <summary>
    /// 聊天消息
    /// </summary>
    public class ChatMessage : MessageModel
    {
        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("isPrivate")]
        public bool IsPrivate { get; set; }

        [JsonProperty("targetUser")]
        public string TargetUser { get; set; }

        public ChatMessage()
        {
            Type = MessageType.Chat;
        }
    }

    /// <summary>
    /// 广播消息
    /// </summary>
    public class BroadcastMessage : MessageModel
    {
        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("priority")]
        public string Priority { get; set; }

        public BroadcastMessage()
        {
            Type = MessageType.Broadcast;
        }
    }

    /// <summary>
    /// 心跳消息
    /// </summary>
    public class HeartbeatMessage : MessageModel
    {
        [JsonProperty("clientStatus")]
        public string ClientStatus { get; set; }

        public HeartbeatMessage()
        {
            Type = MessageType.Heartbeat;
        }
    }

    /// <summary>
    /// 错误消息
    /// </summary>
    public class ErrorMessage : MessageModel
    {
        [JsonProperty("errorCode")]
        public string ErrorCode { get; set; }

        public ErrorMessage()
        {
            Type = MessageType.Error;
        }
    }

    /// <summary>
    /// 用户信息
    /// </summary>
    public class UserInfo
    {
        [JsonProperty("username")]
        public string Username { get; set; }

        [JsonProperty("displayName")]
        public string DisplayName { get; set; }

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("ipAddress")]
        public string IpAddress { get; set; }

        [JsonProperty("connectTime")]
        public string ConnectTime { get; set; }

        [JsonProperty("lastActivity")]
        public string LastActivity { get; set; }

        [JsonProperty("statusMessage")]
        public string StatusMessage { get; set; }
    }

    /// <summary>
    /// 用户列表响应消息
    /// </summary>
    public class UserListResponseMessage : MessageModel
    {
        [JsonProperty("totalCount")]
        public int TotalCount { get; set; }

        [JsonProperty("users")]
        public List<UserInfo> Users { get; set; }

        public UserListResponseMessage()
        {
            Type = MessageType.UserListResponse;
            Users = new List<UserInfo>();
        }
    }

    /// <summary>
    /// 用户状态更新消息
    /// </summary>
    public class UserStatusUpdateMessage : MessageModel
    {
        [JsonProperty("username")]
        public string Username { get; set; }

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("statusMessage")]
        public string StatusMessage { get; set; }

        public UserStatusUpdateMessage()
        {
            Type = MessageType.UserStatusUpdate;
        }
    }

    /// <summary>
    /// 服务器命令消息
    /// </summary>
    public class ServerCommandMessage : MessageModel
    {
        [JsonProperty("command")]
        public string Command { get; set; }

        [JsonProperty("parameters")]
        public object Parameters { get; set; }

        public ServerCommandMessage()
        {
        Type = MessageType.ServerCommand;
        }
    }

    /// <summary>
    /// 断开连接消息
    /// </summary>
    public class DisconnectMessage : MessageModel
    {
        [JsonProperty("reason")]
        public string Reason { get; set; }

        public DisconnectMessage()
        {
            Type = MessageType.Disconnect;
        }
    }
}